﻿using MLRobotic.Tower.Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MLRobotic.Tower.Detection;
namespace MLRobotic.Tower.Test
{
    [TestClass()]
    public class BitmapHelperTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod()]
        public void ConvertRGB16To24Then24To16_ForAllColors_IsInvariant()
        {
            for (int high = 255; high >= 0; high--)
            {
                for (int low = 255; low >= 0; low--)
                {
                    byte r, g, b;
                    byte high2, low2;

                    BitmapHelper.Convert565ToRGB((byte)low, (byte)high, out r, out g, out b);
                    BitmapHelper.ConvertRGBTo565(r, g, b, out low2, out high2);

                    Assert.AreEqual(high, high2);
                    Assert.AreEqual(low, low2);
                }
            }
        }

        [TestMethod()]
        public void ConvertRGB16To24_ForAllColors_IsInvariant()
        {
            for (int high = 255; high >= 0; high--)
            {
                for (int low = 255; low >= 0; low--)
                {
                    byte r, g, b;
                    byte r2, g2, b2;
                    byte[] buffer = new byte[2];
                    buffer[0] = (byte)low;
                    buffer[1] = (byte)high;

                    BitmapHelper.Convert565ToRGB((byte)low, (byte)high, out r, out g, out b);
                    BitmapHelper.Convert565ToRGB(buffer, 0, out r2, out g2, out b2);

                    Assert.AreEqual(r, r2);
                    Assert.AreEqual(g, g2);
                    Assert.AreEqual(b, b2);
                }
            }
        }

        [TestMethod()]
        public void ConvertRGB24To16_ForAllColors_ReturnsHighBits()
        {
            for (int r = 255; r >= 0; r--)
            {
                for (int g = 255; g >= 0; g--)
                {
                    for (int b = 255; b >= 0; b--)
                    {
                        byte high, low;
                        BitmapHelper.ConvertRGBTo565((byte)r, (byte)g, (byte)b, out low, out high);

                        Assert.AreEqual(r & 0xF8, high & 0xF8); // 5 higher
                        Assert.AreEqual(g & 0xFC, (high << 5 | low >> 3) & 0xFC); // 6 higher
                        Assert.AreEqual(b & 0xF8, (low << 3) & 0xF8); // 5 higher
                    }
                }
            }
        }
    }
}
